<?php
namespace TennisTournament\Model;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="GROUP_STATS")
 */
class GroupStats {

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @var integer
     */
    protected $id;

    /**
     * @ORM\Column(type="smallint")
     * @var integer
     */
    protected $points;

    /**
     * @ORM\Column(name="taken_points", type="integer")
     * @var integer
     */
    protected $takenPoints;

    /**
     * @ORM\Column(name="get_points", type="integer")
     * @var integer
     */
    protected $getPoints;

    /**
     * @ORM\Column(name="count_victories", type="integer")
     * @var integer
     */
    protected $countVictories;

    /**
     * @ORM\Column(name="count_losses", type="integer")
     * @var integer
     */
    protected $countLosses;

    /**
     * @ORM\OneToOne(targetEntity="Player", inversedBy="groupStats")
     * @ORM\JoinColumn(name="player_id", referencedColumnName="id", unique=true)
     * @var Player
     */
    protected $player;

    /**
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="stats")
     * @ORM\JoinColumn(name="group_name", referencedColumnName="name")
     * @var Group
     */
    protected $group;

    public function __construct($data = array()) {
        if (count($data) > 0) {
            $this->exchangeArray($data);
        }
    }

    /**
     * @param int $countLosses
     */
    public function setCountLosses($countLosses) {
        $this->countLosses = $countLosses;
    }

    /**
     * @return int
     */
    public function getCountLosses() {
        return $this->countLosses;
    }

    /**
     * @param int $countVictories
     */
    public function setCountVictories($countVictories) {
        $this->countVictories = $countVictories;
    }

    /**
     * @return int
     */
    public function getCountVictories() {
        return $this->countVictories;
    }

    /**
     * @param int $getPoints
     */
    public function setGetPoints($getPoints) {
        $this->getPoints = $getPoints;
    }

    /**
     * @return int
     */
    public function getGetPoints() {
        return $this->getPoints;
    }

    /**
     * @param \TennisTournament\Model\Group $group
     */
    public function setGroup(Group $group) {
        $this->group = $group;
    }

    /**
     * @return \TennisTournament\Model\Group
     */
    public function getGroup() {
        return $this->group;
    }

    /**
     * @param int $id
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param \TennisTournament\Model\Player $player
     */
    public function setPlayer($player) {
        $this->player = $player;
    }

    /**
     * @return \TennisTournament\Model\Player
     */
    public function getPlayer() {
        return $this->player;
    }

    /**
     * @param int $points
     */
    public function setPoints($points) {
        $this->points = $points;
    }

    /**
     * @return int
     */
    public function getPoints() {
        return $this->points;
    }

    /**
     * @param int $takenPoints
     */
    public function setTakenPoints($takenPoints) {
        $this->takenPoints = $takenPoints;
    }

    /**
     * @return int
     */
    public function getTakenPoints() {
        return $this->takenPoints;
    }

    /**
     * Convert a Player into an associative array
     * @return array
     */
    public function toArray() {
        return array(
            'id'             => $this->getId(),
            'player'         => $this->getPlayer()->toArray(),
            'countLosses'    => $this->getCountLosses(),
            'countVictories' => $this->getCountVictories(),
            'getPoints'      => $this->getGetPoints(),
            'takenPoints'    => $this->getTakenPoints(),
            'points'         => $this->getPoints()
        );
    }

    /**
     * Create a {@link GroupStats} object as populate the values
     * parsed from $data array
     * @param array $data
     * @return GroupStats
     */
    public function exchangeArray($data = array()) {

        if (isset($data['id']) && ($this->id != $data['id'])) {
            $this->id = $data['id'];
        }

        if (isset($data['countLosses']) && ($this->countLosses != $data['countLosses'])) {
            $this->countLosses = $data['countLosses'];
        } else {
            $this->countLosses = 0;
        }

        if (isset($data['countVictories']) && ($this->countVictories != $data['countVictories'])) {
            $this->countVictories = $data['countVictories'];
        } else {
            $this->countVictories = 0;
        }

        if (isset($data['getPoints']) && ($this->getPoints != $data['getPoints'])) {
            $this->getPoints = $data['getPoints'];
        } else {
            $this->getPoints = 0;
        }

        if (isset($data['takenPoints']) && ($this->takenPoints != $data['takenPoints'])) {
            $this->takenPoints = $data['takenPoints'];
        } else {
            $this->takenPoints = 0;
        }

        if (isset($data['points']) && ($this->points != $data['points'])) {
            $this->points = $data['points'];
        } else {
            $this->points = 0;
        }

        if (isset($data['player']) && is_array($data['player'])) {
            $player = new Player($data['player']);
            $player->setGroupStats($this);
            $this->player = $player;
        }

        return $this;
    }
}